package com.biz.hunter.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.biz.hunter.dao.CompanyDAO;
import com.biz.hunter.dao.EmployeeDAO;
import com.biz.hunter.dao.JobDAO;
import com.biz.hunter.dao.JobTitleDAO;
import com.biz.hunter.dao.PersonDAO;
import com.biz.hunter.db.entity.Company;
import com.biz.hunter.db.entity.Employee;
import com.biz.hunter.db.entity.JobDesc;
import com.biz.hunter.db.entity.JobTitle;
import com.biz.hunter.db.entity.Person;
import com.biz.hunter.service.IJobDescService;

@Service
public class JobDescService implements IJobDescService {

	public static final int PAGE_SIZE = 8;
	private Logger logger = LoggerFactory.getLogger(JobDescService.class);
	@Autowired
	private JobDAO jobDAO;
	@Autowired
	private PersonDAO personDAO;
	@Autowired
	private EmployeeDAO empDAO;
	@Autowired
	private JobTitleDAO jobTitleDAO;
	@Autowired
  private CompanyDAO companyDAO;
	@Override
	public List<JobDesc> searchJobByKeyword(String... name) {
		// jobDAO.getJobDescList(name);
		return jobDAO.getJobDescList(name);
	}

	@Override
	public JobDesc searchJobByID(Integer Id) {
		return jobDAO.getById(Id);
	}

	@Override
	public String getLatestMedia(Integer domainId) {
		// TODO Auto-generated method stub
		return "";
	}

	@Override
	public List<JobDesc> searchJobsWithReward() {
		return jobDAO.getJobsWithReward();
	}

	@Override
	public List<JobDesc> searchJobsWithReward(int platformId) {
		return jobDAO.getJobsWithReward(platformId);

	}

	@Override
	public List<JobDesc> searchJobByKeyword(Integer start, Integer end,
			String... keywords) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<JobDesc> searchJobsWithReward(Integer start, Integer end) {
		// TODO Auto-generated method stub
		return null;
	}

//	@Override
//	public List<JobDesc> searchAssistantJobs4Candidate(Integer domainId,
//			Integer offset) {
//
//		return searchJobs4Candidate(domainId, offset, PAGE_SIZE);
//	}

	public List<JobDesc> searchRecommendJobs4Candidate(Integer domainId, Integer offset) {
		return jobDAO.getJobs4Candidate(domainId, offset,  PAGE_SIZE);
	}

	public List<JobDesc> searchRecommendJobs4Candidate2(Integer domainId, Integer offset, long today) {
	// TODO:推荐的职位应该是1.相关个人的职位和期望来源 2以及职位希望来源公司与注册用户公司一致的，
    // searchassistantjobs4candidate2
//    Integer expectJob = sessionEmp.getExpectJob();
//    String companys = sessionEmp.getExpectCompanies();
//    int companyId = sessionEmp.getCompanyId();
    
    // TODO:
	  
	  return jobDAO.getJobs4Candidate2(domainId, offset,  PAGE_SIZE, today);
	}
	
	private List<JobDesc> searchJobs4Candidate(Integer domainId, Integer offset, int pagesize) {
		return jobDAO.getJobs4Candidate(domainId, offset, pagesize);
	}

	@Override
	public List<JobDesc> searchAssistantJobs4Candidate(Employee emp, Integer offset) {
		int domainId = emp.getDomainId();
		int companyId1 = emp.getExpectCompany1();
		int companyId2 = emp.getExpectCompany2();
		int companyId3 = emp.getExpectCompany3();
		String conditionStr= ""+ companyId1;
		StringBuilder sb = new StringBuilder();
		sb.append("");
		if (companyId1!=0)
		{
			sb.append(companyId1);
		}
		if (companyId2!=0)
		{
			sb.append(",");
			sb.append(companyId2);
		}
		if (companyId3!=0)
		{
			sb.append(",");
			sb.append(companyId3);
		}
		sb.append("");
		conditionStr = sb.toString();
		logger.debug("company condition is"+ conditionStr);
		
		int jobid = emp.getExpectJob();
		JobTitle jt= jobTitleDAO.getById(jobid);
		if (jt==null){return null;}
				//匹配领域，职位，公司
		return jobDAO.searchJobs4Candidate(domainId,jt.getName(),conditionStr, offset, PAGE_SIZE);
	}
	
	
	public List<JobDesc> searchAssistantJobs4Candidate2(Employee emp, Integer offset, long today) {
		int domainId = emp.getDomainId();
		int companyId1 = emp.getExpectCompany1();
		int companyId2 = emp.getExpectCompany2();
		int companyId3 = emp.getExpectCompany3();
		String conditionStr= ""+ companyId1;
		StringBuilder sb = new StringBuilder();
		sb.append("");
		if (companyId1!=0)
		{
			sb.append(companyId1);
		}
		if (companyId2!=0)
		{
			sb.append(",");
			sb.append(companyId2);
		}
		if (companyId3!=0)
		{
			sb.append(",");
			sb.append(companyId3);
		}
		sb.append("");
		conditionStr = sb.toString();
		logger.debug("company condition is "+ conditionStr);
		
		int jobid = emp.getExpectJob();
		JobTitle jt= jobTitleDAO.getById(jobid);
		if (jt==null){
		  logger.debug("no job match the jobid "+ jobid);
		  return null;
		  }
		
				
		return jobDAO.searchJobs4Candidate2(domainId,jt.getName(),conditionStr, offset, PAGE_SIZE, today);
	}

  @Override
  public List<JobDesc> searchRecommendJobs4Candidate(Employee emp, Integer offset, long today) {
   //推荐的职位应该是1.相关个人的职位和期望来源 2以及职位希望来源公司与注册用户公司一致的，
      // searchassistantjobs4candidate2
	  Person person=personDAO.getById(emp.getPersonId());
      Integer expectJob = emp.getExpectJob();
      long roleId = person.getRoleId();
    
      String companys = emp.getExpectCompanies();
      int companyId = person.getCompanyId();
      int majorDomainId1 = emp.getMajorDomainId1();
      int majorDomainId2 = emp.getMajorDomainId2();
      int majorDomainId3 = emp.getMajorDomainId3();
//      Company com = companyDAO .getById(companyId);
//      int domainId = com.getDomain();
      int domainId = emp.getDomainId();
      int cityId= person.getCityId();
      
      return jobDAO.searchJobsByDomain(expectJob,roleId, companyId, companys, 
                                          majorDomainId1, majorDomainId2,majorDomainId3,
                                          today, cityId,
                                          offset,  PAGE_SIZE);
    }

@Override
public List<JobDesc> findFavorJobs(String openid) {
	return jobDAO.searchUserJobs(openid);
}

 
}
